<script>
	import auth from '@/common/js/auth.js';
	// #ifdef H5
	import {
		Weixin
	} from '@/common/js/wx-jssdk.js';
	// #endif
	export default {
		mixins: [auth],
		onLaunch: function() {
			uni.hideTabBar();

			// #ifdef MP
			const updateManager = uni.getUpdateManager();
			updateManager.onCheckForUpdate(function(res) {
				// 请求完新版本信息的回调
			});

			updateManager.onUpdateReady(function(res) {
				uni.showModal({
					title: '更新提示',
					content: '新版本已经准备好，是否重启应用？',
					success(res) {
						if (res.confirm) {
							// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
							updateManager.applyUpdate();
						}
					},
				});
			});

			updateManager.onUpdateFailed(function(res) {
				// 新的版本下载失败
			});
			// #endif

			// uni.getLocation({
			// 	type: 'gcj02',
			// 	success: res => {
			// 		let obj = uni.getStorageSync('location');
			// 		if (obj) {
			// 			let a = this.$util.getDistance(obj.latitude, obj.longitude, res.latitude, res.longitude);
			// 			if (a > 20) {
			// 				uni.removeStorageSync('store');
			// 			}
			// 		}
			// 		uni.setStorage({
			// 			key: 'location',
			// 			data: {
			// 				latitude: res.latitude,
			// 				longitude: res.longitude
			// 			}
			// 		});
			// 	}
			// });

			// #ifdef H5
			if (uni.getSystemInfoSync().platform == 'ios') {
				uni.setStorageSync('initUrl', location.href);
			}
			// #endif

			uni.onNetworkStatusChange(function(res) {
				if (!res.isConnected) {
					uni.showModal({
						title: '网络失去链接',
						content: '请检查网络链接',
						showCancel: false
					});
				}
			});

			if (this.$util.isWeiXin()) {
				setTimeout(() => {
					this.sharehide();
				}, 1000)
			}

		},
		onShow: function() {

			this.$store.dispatch('init');

			// 自动登录
			if (!uni.getStorageSync('token') && !uni.getStorageSync('loginLock') && !uni.getStorageSync('unbound')) {
				// #ifdef H5
				if (this.$util.isWeiXin()) {
					this.$util.getUrlCode(urlParams => {
						if (urlParams.source_member) uni.setStorageSync('source_member', urlParams
							.source_member);

						if (urlParams.code == undefined) {

							this.$api.sendRequest({
								url: '/wechat/api/wechat/authcode',
								data: {
									redirect_url: location.href
								},
								success: res => {
									if (res.code >= 0) {
										location.href = res.data;
									} else {
										this.$util.showToast({
											title: res.message || '公众号配置错误'
										});
									}
								}
							});

						} else {
							this.$api.sendRequest({
								url: '/wechat/api/wechat/authcodetoopenid',
								data: {
									code: urlParams.code
								},
								success: res => {
									if (res.code >= 0) {
										let data = {};
										if (res.data.openid) data.wx_openid = res.data.openid;
										if (res.data.unionid) data.wx_unionid = res.data.unionid;
										if (res.data.userinfo) Object.assign(data, res.data
											.userinfo);
										this.authLogin(data);
									}
								}
							});
						}
					});
				}
				// #endif
			}
		},
		onHide: function() {},

		methods: {
			/** 
			 * 授权登录
			 */
			authLogin(data) {
				uni.setStorage({
					key: 'authInfo',
					data: data
				});

				if (uni.getStorageSync('source_member')) data.source_member = uni.getStorageSync('source_member');

				this.$api.sendRequest({
					url: '/api/login/auth',
					data,
					success: res => {
						if (res.code >= 0) {
							uni.setStorage({
								key: 'token',
								data: res.data.token,
								success: () => {
									this.$store.dispatch('getCartNumber');
									this.$store.commit('setToken', res.data.token);
								}
							});
						} else {
							uni.setStorage({
								key: 'unbound',
								data: 1,
								success: () => {}
							});
						}
					}
				});
			},
			/**
			 * 公众号分享隐藏
			 */
			sharehide() {
				this.$api.sendRequest({
					url: '/wechat/api/wechat/share',
					data: {
						url: window.location.href
					},
					success: res => {
						if (res.code == 0) {
							var wxJS = new Weixin();
							wxJS.init(res.data.jssdk_config);

							let shareConfig = res.data.share_config,
								url = this.$config.h5Domain + '/pages/goods/detail/detail?sku_id=' + this
								.skuId;
							if (this.memberId) url += '&source_member=' + this.memberId;
							let share_data = JSON.parse(JSON.stringify(res.data.share_config.data));
							if (share_data) {

								wxJS.setShareData({
									title: share_data.title,
									desc: share_data.desc,
									link: share_data.link,
									imgUrl: this.$util.img(share_data.imgUrl)
								}, (res) => {
									console.log(res);
								})
							}

							let hideOptionMenu = res.data.share_config.permission.hideOptionMenu;
							let hideMenuItems = res.data.share_config.permission.hideMenuItems;

							if (hideOptionMenu) {
								wxJS.wexin.hideOptionMenu(); //屏蔽分享好友等按钮
							} else {
								wxJS.wexin.showOptionMenu(); //放开分享好友等按钮
							}

						}
					},
					fail: err => {
						uni.showToast({
							title: "err判断有接口失败",
							icon: "none",
							duration: 1000 * 5
						})
					}
				})
			},
		},
		computed: {

		},
		watch: {
			$route: {
				handler(newName, oldName) {
					if (this.$util.isWeiXin()) {
						this.sharehide();
					}
				},
				// 代表在wacth里声明了firstName这个方法之后立即先去执行handler方法
				immediate: true
			}
		}
	};
</script>
<style lang="scss">
	@import url('/common/css/iconfont.css');
	@import './common/css/main.scss';
</style>